package pers.amos.eshop.request.support;

import pers.amos.eshop.model.ProductInventory;
import pers.amos.eshop.request.Request;
import pers.amos.eshop.service.ProductInventoryService;

/**
 * @author amos wong
 * @create 2020-11-14 4:14 下午
 */

public class ProductInventoryRefreshCacheRequest implements Request {

    private Long productId;

    private ProductInventoryService productInventoryService;

    /**
     * 是否强制刷新缓存，避免BUG
     */
    private Boolean forceFresh;

    public ProductInventoryRefreshCacheRequest(Long productId, ProductInventoryService productInventoryService, Boolean forceFresh) {
        this.productId = productId;
        this.productInventoryService = productInventoryService;
        this.forceFresh = forceFresh;
    }

    /**
     * 重新将商品库存信息加载到缓存
     * 1.从数据库中查询商品库存信息
     * 2.将库存信息放入缓存中
     */
    @Override
    public void process() {
        ProductInventory productInventory = productInventoryService.findProductInventoryByProductId(productId);
        System.out.println("===========日志===========: 已查询到商品最新的库存数量，商品id=" + productId + ", 商品库存数量=" + productInventory.getInventoryCnt());
        productInventoryService.setProductInventoryCache(productInventory);
    }

    @Override
    public Long getProductId() {
        return productId;
    }

    @Override
    public Boolean isForceRefresh() {
        return this.forceFresh;
    }
}
